/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;
import Entidad.*;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author ANDREZ
 */
public class HorarioDao {
       private static HorarioDao INSTANCE = new HorarioDao();
    private HorarioDao() {
    }
    public static HorarioDao getInstance(){
    return INSTANCE;
    }
    
    public List<CursoBE> ListarTodosCursos(int IdGrado)
    {
        List<CursoBE> lista = new ArrayList();
        String sql ="{call Curso_ListarTodos_pa(?)}";
        
        Connection c = new bd().getConnectionSQL();
        CallableStatement cs;
        ResultSet rs;
        
        try {
            cs = c.prepareCall(sql);
            cs.setInt(1, IdGrado);
            rs = cs.executeQuery();
            while(rs.next())
            {
                  CursoBE curso  = new CursoBE();
                  
                    curso.setId(rs.getInt("IdCurso"));
                    curso.setNombre(rs.getString("Nombre"));
                    curso.setCargahoraria(rs.getInt("CargaHoraria"));
                    BaseBE area = new BaseBE();
                    area.setId(rs.getInt("IdArea"));
                    area.setNombre(rs.getString("Area"));
                    curso.setArea(area);
                
                    lista.add(curso);
            }
        } catch (SQLException ex) {
            Logger.getLogger(HorarioDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return  lista;
    }
    
     public List<GradoBE> ListarGradoByTipoNivel(int IdTipoNivel)
    {
        List<GradoBE> lista = new ArrayList();
        String sql ="{call Grado_ListarByIdTipoNivel_pa(?)}";
        
        Connection c = new bd().getConnectionSQL();
        CallableStatement cs;
        ResultSet rs;
        
        try {
            cs = c.prepareCall(sql);
            cs.setInt(1, IdTipoNivel);
            rs = cs.executeQuery();
            while(rs.next())
            {
                  GradoBE g  = new GradoBE();
                  g.setId(rs.getInt("IdGrado"));
                  g.setGrado(rs.getInt("Grado"));
                  lista.add(g);
            }
        } catch (SQLException ex) {
            Logger.getLogger(HorarioDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return  lista;
    }
     
      public Boolean InsertarMalla(MallaBE malla)
    {
        Boolean Success= false;
        String sql ="{call Malla_Insert_pa(?)}";
        Connection c = new bd().getConnectionSQL();
        CallableStatement cs;
        
        try {
            cs = c.prepareCall(sql);
            cs.setString(1, malla.getDescripcion());
            cs.executeQuery();
            
            Success = true;
        } catch (SQLException ex) {
            Logger.getLogger(HorarioDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return  Success;
    }
      
      public  Boolean UpdateSucesivo(MallaBE malla){
          
       Boolean Success=false;
       for(CursoBE curso: malla.getLista()){
           curso.setMalla(malla);
           Success = CursoUpdateMalla(curso);           
       }
       
       return  Success;
      }
      
      public Boolean CursoUpdateMalla(CursoBE curso)
    {
        Boolean Success= false;
        String sql ="{call Curso_UpdateMalla_pa(?,?)}";
        
        Connection c = new bd().getConnectionSQL();
        CallableStatement cs;
        
        try {
            cs = c.prepareCall(sql);
            cs.setInt(1, curso.getId());
            cs.setInt(2, curso.getMalla().getId());
            cs.executeQuery();
            Success = true;
        } catch (SQLException ex) {
            Logger.getLogger(HorarioDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return  Success;
    }
      
      public int GetLastMalla(){        
        
        String sql ="{call Malla_LastMalla_pa(?)}";
        
        Connection c = new bd().getConnectionSQL();
        CallableStatement cs;
        ResultSet rs;
        int retorno = 0;
        
        try {
            cs = c.prepareCall(sql);
            cs.setInt(1, retorno);
            rs = cs.executeQuery();
            while(rs.next())
            {
                retorno =rs.getInt("id");
            }
        } catch (SQLException ex) {
            Logger.getLogger(HorarioDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return retorno;
      
      }
      
}
